package com.xrit.sunnyday.controller;

import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.utils.SMSUtils;
import com.xrit.sunnyday.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 发送手机验证码
     */
    @PostMapping("/send4Order")
    public Result send4Order(String telephone) {
        //1.生成验证码之前先查看Redis,查看是否存在此key
        Jedis jedis = jedisPool.getResource();
        //2.value对应的是生成的验证码code
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //3.redis中的验证码
        String codeInRedis = jedis.get(key);
        //4.不存在或者没发送就发送验证码
        if (null == codeInRedis) {
            //5.生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            //腾讯云需要String[]来替代短信模板的参数
            String[] params = new String[]{code+""};
            try {
                //6.调用SMSUtils来发送验证码
                SMSUtils.sendSingleMessageForCode(telephone, params);
                //7.把验证码存入redis设置5分钟内有效期，value为验证码, key为手机号码
                jedis.setex(key, 5*60, code + "");
                // 8.验证码发送成功
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                //9.验证码发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        //10.存在就发送验证码
        return new Result(false, MessageConstant.SENT_VALIDATECODE);
    }

    /**
     * 发送登录手机验证码
     */
    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        //1.通过redis获取key看是否存在
        Jedis jedis = jedisPool.getResource();
        //2.value对应的是生成的验证码code
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        //3.redis中的验证码
        String codeInRedis = jedis.get(key);
        //4.不存在或者没发送就发送验证码
        if (null == codeInRedis) {
            //5.生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            //腾讯云需要String[]来替代短信模板的参数
            String[] params = new String[]{code+""};
            try {
                //6.调用SMSUtils来发送验证码
                SMSUtils.sendSingleMessageForCode(telephone, params);
                //7.把验证码存入redis设置5分钟内有效期，value为验证码, key为手机号码
                jedis.setex(key, 5*60, code + "");
                // 8.验证码发送成功
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                //9.验证码发送失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
        }
        //10.存在就发送验证码
        return new Result(false, MessageConstant.SENT_VALIDATECODE);
    }
}